{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "78e0a53b-38d5-4d9f-82d3-2364e617acd0",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (2438209256.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[9], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple\u001b[0m\n\u001b[1;37m        ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8740b588-12dd-4304-aac5-4b0adce6b84f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: ultralytics in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (8.3.40)\n",
      "Requirement already satisfied: numpy>=1.23.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (1.26.4)\n",
      "Requirement already satisfied: matplotlib>=3.3.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (3.8.4)\n",
      "Requirement already satisfied: opencv-python>=4.6.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (4.10.0.84)\n",
      "Requirement already satisfied: pillow>=7.1.2 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (10.4.0)\n",
      "Requirement already satisfied: pyyaml>=5.3.1 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (6.0.1)\n",
      "Requirement already satisfied: requests>=2.23.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (2.32.3)\n",
      "Requirement already satisfied: scipy>=1.4.1 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (1.13.1)\n",
      "Requirement already satisfied: torch>=1.8.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (2.5.1)\n",
      "Requirement already satisfied: torchvision>=0.9.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (0.20.1)\n",
      "Requirement already satisfied: tqdm>=4.64.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (4.67.1)\n",
      "Requirement already satisfied: psutil in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (5.9.0)\n",
      "Requirement already satisfied: py-cpuinfo in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (9.0.0)\n",
      "Requirement already satisfied: pandas>=1.1.4 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (2.2.2)\n",
      "Requirement already satisfied: seaborn>=0.11.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (0.13.2)\n",
      "Requirement already satisfied: ultralytics-thop>=2.0.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from ultralytics) (2.0.12)\n",
      "Requirement already satisfied: contourpy>=1.0.1 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from matplotlib>=3.3.0->ultralytics) (1.2.0)\n",
      "Requirement already satisfied: cycler>=0.10 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from matplotlib>=3.3.0->ultralytics) (0.11.0)\n",
      "Requirement already satisfied: fonttools>=4.22.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from matplotlib>=3.3.0->ultralytics) (4.51.0)\n",
      "Requirement already satisfied: kiwisolver>=1.3.1 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from matplotlib>=3.3.0->ultralytics) (1.4.4)\n",
      "Requirement already satisfied: packaging>=20.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from matplotlib>=3.3.0->ultralytics) (24.1)\n",
      "Requirement already satisfied: pyparsing>=2.3.1 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from matplotlib>=3.3.0->ultralytics) (3.0.9)\n",
      "Requirement already satisfied: python-dateutil>=2.7 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from matplotlib>=3.3.0->ultralytics) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from pandas>=1.1.4->ultralytics) (2024.1)\n",
      "Requirement already satisfied: tzdata>=2022.7 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from pandas>=1.1.4->ultralytics) (2023.3)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from requests>=2.23.0->ultralytics) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from requests>=2.23.0->ultralytics) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from requests>=2.23.0->ultralytics) (2.2.2)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from requests>=2.23.0->ultralytics) (2024.7.4)\n",
      "Requirement already satisfied: filelock in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from torch>=1.8.0->ultralytics) (3.13.1)\n",
      "Requirement already satisfied: typing-extensions>=4.8.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from torch>=1.8.0->ultralytics) (4.11.0)\n",
      "Requirement already satisfied: networkx in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from torch>=1.8.0->ultralytics) (3.3)\n",
      "Requirement already satisfied: jinja2 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from torch>=1.8.0->ultralytics) (3.1.4)\n",
      "Requirement already satisfied: fsspec in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from torch>=1.8.0->ultralytics) (2024.10.0)\n",
      "Requirement already satisfied: sympy==1.13.1 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from torch>=1.8.0->ultralytics) (1.13.1)\n",
      "Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from sympy==1.13.1->torch>=1.8.0->ultralytics) (1.3.0)\n",
      "Requirement already satisfied: colorama in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from tqdm>=4.64.0->ultralytics) (0.4.6)\n",
      "Requirement already satisfied: six>=1.5 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from python-dateutil>=2.7->matplotlib>=3.3.0->ultralytics) (1.16.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in c:\\programdata\\anaconda3\\envs\\machinelearning\\lib\\site-packages (from jinja2->torch>=1.8.0->ultralytics) (2.1.3)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install ultralytics -i https://pypi.tuna.tsinghua.edu.cn/simple\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "88bdebf8-3b6c-40d0-827e-af6540ffa359",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ultralytics import YOLO\n",
    "\n",
    "# 加载模型\n",
    "model = YOLO(\"yolov8n-pose.pt\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "48f6a8af-6000-4085-814a-88786ec7ef4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "\n",
    "image_path = 'image.jpg'\n",
    "img = Image.open(image_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e614306b-fdbc-4341-a8b4-dd2f36f152dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 480x640 1 person, 71.8ms\n",
      "Speed: 1.0ms preprocess, 71.8ms inference, 0.0ms postprocess per image at shape (1, 3, 480, 640)\n",
      "[ultralytics.engine.results.Results object with attributes:\n",
      "\n",
      "boxes: ultralytics.engine.results.Boxes object\n",
      "keypoints: ultralytics.engine.results.Keypoints object\n",
      "masks: None\n",
      "names: {0: 'person'}\n",
      "obb: None\n",
      "orig_img: array([[[13,  8,  7],\n",
      "        [14,  9,  8],\n",
      "        [15, 10,  9],\n",
      "        ...,\n",
      "        [ 9, 18, 62],\n",
      "        [ 6, 13, 52],\n",
      "        [ 7, 10, 48]],\n",
      "\n",
      "       [[14,  9,  8],\n",
      "        [15, 10,  9],\n",
      "        [15, 11, 10],\n",
      "        ...,\n",
      "        [10, 18, 65],\n",
      "        [ 9, 15, 56],\n",
      "        [ 8, 14, 51]],\n",
      "\n",
      "       [[14,  9, 10],\n",
      "        [15, 10, 11],\n",
      "        [16, 11, 12],\n",
      "        ...,\n",
      "        [12, 20, 67],\n",
      "        [11, 17, 60],\n",
      "        [10, 15, 54]],\n",
      "\n",
      "       ...,\n",
      "\n",
      "       [[ 8,  9,  7],\n",
      "        [ 8,  9,  7],\n",
      "        [ 8,  9,  7],\n",
      "        ...,\n",
      "        [ 6,  9,  7],\n",
      "        [ 6,  9,  7],\n",
      "        [ 6,  9,  7]],\n",
      "\n",
      "       [[ 8,  9,  7],\n",
      "        [ 8,  9,  7],\n",
      "        [ 8,  9,  7],\n",
      "        ...,\n",
      "        [ 6,  9,  7],\n",
      "        [ 6,  9,  7],\n",
      "        [ 6,  9,  7]],\n",
      "\n",
      "       [[ 8,  9,  7],\n",
      "        [ 8,  9,  7],\n",
      "        [ 8,  9,  7],\n",
      "        ...,\n",
      "        [ 6,  9,  7],\n",
      "        [ 6,  9,  7],\n",
      "        [ 6,  9,  7]]], dtype=uint8)\n",
      "orig_shape: (284, 400)\n",
      "path: 'C:\\\\Users\\\\Administrator\\\\image.jpg'\n",
      "probs: None\n",
      "save_dir: 'runs\\\\pose\\\\predict'\n",
      "speed: {'preprocess': 0.9961128234863281, 'inference': 71.807861328125, 'postprocess': 0.0}]\n"
     ]
    }
   ],
   "source": [
    "results = model(img)\n",
    "print(results)  # 直接打印列表内容\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7d033e52-5676-4e0d-b0ba-2498e6fc95b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0: 384x640 3 persons, 51.9ms\n",
      "Speed: 3.0ms preprocess, 51.9ms inference, 1.0ms postprocess per image at shape (1, 3, 384, 640)\n",
      "ultralytics.engine.results.Keypoints object with attributes:\n",
      "\n",
      "conf: tensor([[9.9572e-01, 9.8188e-01, 9.9614e-01, 5.5387e-01, 9.7508e-01, 9.5307e-01, 9.8532e-01, 8.7328e-02, 2.9981e-01, 6.6093e-02, 2.4273e-01, 9.3254e-03, 1.6090e-02, 1.4133e-03, 2.1969e-03, 6.5630e-04, 8.8693e-04],\n",
      "        [9.1853e-01, 8.4864e-01, 8.6148e-01, 4.5910e-01, 5.3833e-01, 9.1473e-01, 8.4486e-01, 7.2722e-01, 5.2185e-01, 6.4535e-01, 5.0159e-01, 8.0861e-01, 7.3968e-01, 6.6255e-01, 5.7488e-01, 3.9331e-01, 3.2936e-01],\n",
      "        [9.4191e-01, 9.0719e-01, 9.2127e-01, 6.1171e-01, 6.5811e-01, 8.7021e-01, 8.8976e-01, 4.3558e-01, 5.0310e-01, 3.6286e-01, 4.1339e-01, 3.0514e-01, 3.3475e-01, 6.9517e-02, 7.7195e-02, 3.0360e-02, 3.1556e-02]])\n",
      "data: tensor([[[1.0303e+03, 2.2231e+02, 9.9572e-01],\n",
      "         [1.0851e+03, 1.6002e+02, 9.8188e-01],\n",
      "         [9.3124e+02, 1.5751e+02, 9.9614e-01],\n",
      "         [1.1432e+03, 2.3996e+02, 5.5387e-01],\n",
      "         [7.6554e+02, 2.4737e+02, 9.7508e-01],\n",
      "         [1.3329e+03, 7.0765e+02, 9.5307e-01],\n",
      "         [5.1675e+02, 6.5396e+02, 9.8532e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 8.7328e-02],\n",
      "         [0.0000e+00, 0.0000e+00, 2.9981e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 6.6093e-02],\n",
      "         [0.0000e+00, 0.0000e+00, 2.4273e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 9.3254e-03],\n",
      "         [0.0000e+00, 0.0000e+00, 1.6090e-02],\n",
      "         [0.0000e+00, 0.0000e+00, 1.4133e-03],\n",
      "         [0.0000e+00, 0.0000e+00, 2.1969e-03],\n",
      "         [0.0000e+00, 0.0000e+00, 6.5630e-04],\n",
      "         [0.0000e+00, 0.0000e+00, 8.8693e-04]],\n",
      "\n",
      "        [[1.7052e+03, 3.8085e+02, 9.1853e-01],\n",
      "         [1.7208e+03, 3.6181e+02, 8.4864e-01],\n",
      "         [1.6818e+03, 3.6117e+02, 8.6148e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 4.5910e-01],\n",
      "         [1.6376e+03, 3.7144e+02, 5.3833e-01],\n",
      "         [1.7659e+03, 4.7024e+02, 9.1473e-01],\n",
      "         [1.6105e+03, 4.7610e+02, 8.4486e-01],\n",
      "         [1.8202e+03, 5.7422e+02, 7.2722e-01],\n",
      "         [1.6261e+03, 6.1235e+02, 5.2185e-01],\n",
      "         [1.8054e+03, 4.8877e+02, 6.4535e-01],\n",
      "         [1.7259e+03, 5.7886e+02, 5.0159e-01],\n",
      "         [1.7681e+03, 7.1720e+02, 8.0861e-01],\n",
      "         [1.6646e+03, 7.2238e+02, 7.3968e-01],\n",
      "         [1.7618e+03, 8.8313e+02, 6.6255e-01],\n",
      "         [1.6758e+03, 8.9222e+02, 5.7488e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 3.9331e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 3.2936e-01]],\n",
      "\n",
      "        [[4.3753e+02, 1.8872e+02, 9.4191e-01],\n",
      "         [4.6048e+02, 1.6556e+02, 9.0719e-01],\n",
      "         [4.1489e+02, 1.6520e+02, 9.2127e-01],\n",
      "         [4.9349e+02, 1.7823e+02, 6.1171e-01],\n",
      "         [3.8246e+02, 1.7768e+02, 6.5811e-01],\n",
      "         [5.5239e+02, 3.0890e+02, 8.7021e-01],\n",
      "         [3.1754e+02, 3.0597e+02, 8.8976e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 4.3558e-01],\n",
      "         [2.7873e+02, 4.6268e+02, 5.0310e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 3.6286e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 4.1339e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 3.0514e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 3.3475e-01],\n",
      "         [0.0000e+00, 0.0000e+00, 6.9517e-02],\n",
      "         [0.0000e+00, 0.0000e+00, 7.7195e-02],\n",
      "         [0.0000e+00, 0.0000e+00, 3.0360e-02],\n",
      "         [0.0000e+00, 0.0000e+00, 3.1556e-02]]])\n",
      "has_visible: True\n",
      "orig_shape: (1080, 1920)\n",
      "shape: torch.Size([3, 17, 3])\n",
      "xy: tensor([[[1030.2579,  222.3101],\n",
      "         [1085.1055,  160.0209],\n",
      "         [ 931.2354,  157.5062],\n",
      "         [1143.1699,  239.9644],\n",
      "         [ 765.5369,  247.3722],\n",
      "         [1332.9116,  707.6468],\n",
      "         [ 516.7458,  653.9628],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000]],\n",
      "\n",
      "        [[1705.1603,  380.8469],\n",
      "         [1720.7875,  361.8073],\n",
      "         [1681.8186,  361.1667],\n",
      "         [   0.0000,    0.0000],\n",
      "         [1637.6451,  371.4442],\n",
      "         [1765.8586,  470.2353],\n",
      "         [1610.5262,  476.0951],\n",
      "         [1820.1735,  574.2153],\n",
      "         [1626.0890,  612.3547],\n",
      "         [1805.4039,  488.7671],\n",
      "         [1725.9362,  578.8555],\n",
      "         [1768.1455,  717.2034],\n",
      "         [1664.6382,  722.3781],\n",
      "         [1761.8302,  883.1255],\n",
      "         [1675.7590,  892.2193],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000]],\n",
      "\n",
      "        [[ 437.5296,  188.7187],\n",
      "         [ 460.4816,  165.5603],\n",
      "         [ 414.8927,  165.1960],\n",
      "         [ 493.4864,  178.2257],\n",
      "         [ 382.4565,  177.6817],\n",
      "         [ 552.3911,  308.9020],\n",
      "         [ 317.5414,  305.9734],\n",
      "         [   0.0000,    0.0000],\n",
      "         [ 278.7318,  462.6828],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000],\n",
      "         [   0.0000,    0.0000]]])\n",
      "xyn: tensor([[[0.5366, 0.2058],\n",
      "         [0.5652, 0.1482],\n",
      "         [0.4850, 0.1458],\n",
      "         [0.5954, 0.2222],\n",
      "         [0.3987, 0.2290],\n",
      "         [0.6942, 0.6552],\n",
      "         [0.2691, 0.6055],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000]],\n",
      "\n",
      "        [[0.8881, 0.3526],\n",
      "         [0.8962, 0.3350],\n",
      "         [0.8759, 0.3344],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.8529, 0.3439],\n",
      "         [0.9197, 0.4354],\n",
      "         [0.8388, 0.4408],\n",
      "         [0.9480, 0.5317],\n",
      "         [0.8469, 0.5670],\n",
      "         [0.9403, 0.4526],\n",
      "         [0.8989, 0.5360],\n",
      "         [0.9209, 0.6641],\n",
      "         [0.8670, 0.6689],\n",
      "         [0.9176, 0.8177],\n",
      "         [0.8728, 0.8261],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000]],\n",
      "\n",
      "        [[0.2279, 0.1747],\n",
      "         [0.2398, 0.1533],\n",
      "         [0.2161, 0.1530],\n",
      "         [0.2570, 0.1650],\n",
      "         [0.1992, 0.1645],\n",
      "         [0.2877, 0.2860],\n",
      "         [0.1654, 0.2833],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.1452, 0.4284],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000],\n",
      "         [0.0000, 0.0000]]])\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "tuple index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[34], line 30\u001b[0m\n\u001b[0;32m     28\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m keypoints \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m     29\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(keypoints\u001b[38;5;241m.\u001b[39mshape[\u001b[38;5;241m1\u001b[39m]):\n\u001b[1;32m---> 30\u001b[0m         x, y, conf \u001b[38;5;241m=\u001b[39m \u001b[43mkeypoints\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m     31\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m conf \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0.5\u001b[39m:  \u001b[38;5;66;03m# 判断关键点的置信度\u001b[39;00m\n\u001b[0;32m     32\u001b[0m             cv2\u001b[38;5;241m.\u001b[39mcircle(frame, (\u001b[38;5;28mint\u001b[39m(x), \u001b[38;5;28mint\u001b[39m(y)), \u001b[38;5;241m5\u001b[39m, (\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m255\u001b[39m), \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)  \u001b[38;5;66;03m# 画圆圈标记关键点\u001b[39;00m\n",
      "File \u001b[1;32mC:\\ProgramData\\anaconda3\\envs\\machinelearning\\lib\\site-packages\\ultralytics\\engine\\results.py:184\u001b[0m, in \u001b[0;36mBaseTensor.__getitem__\u001b[1;34m(self, idx)\u001b[0m\n\u001b[0;32m    167\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__getitem__\u001b[39m(\u001b[38;5;28mself\u001b[39m, idx):\n\u001b[0;32m    168\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    169\u001b[0m \u001b[38;5;124;03m    Returns a new BaseTensor instance containing the specified indexed elements of the data tensor.\u001b[39;00m\n\u001b[0;32m    170\u001b[0m \n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    182\u001b[0m \u001b[38;5;124;03m        tensor([1, 2, 3])\u001b[39;00m\n\u001b[0;32m    183\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m--> 184\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;18;43m__class__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m[\u001b[49m\u001b[43midx\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43morig_shape\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mC:\\ProgramData\\anaconda3\\envs\\machinelearning\\lib\\site-packages\\torch\\utils\\_contextlib.py:116\u001b[0m, in \u001b[0;36mcontext_decorator.<locals>.decorate_context\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    113\u001b[0m \u001b[38;5;129m@functools\u001b[39m\u001b[38;5;241m.\u001b[39mwraps(func)\n\u001b[0;32m    114\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdecorate_context\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m    115\u001b[0m     \u001b[38;5;28;01mwith\u001b[39;00m ctx_factory():\n\u001b[1;32m--> 116\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "File \u001b[1;32mC:\\ProgramData\\anaconda3\\envs\\machinelearning\\lib\\site-packages\\ultralytics\\engine\\results.py:1308\u001b[0m, in \u001b[0;36mKeypoints.__init__\u001b[1;34m(self, keypoints, orig_shape)\u001b[0m\n\u001b[0;32m   1306\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m keypoints\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m2\u001b[39m:\n\u001b[0;32m   1307\u001b[0m     keypoints \u001b[38;5;241m=\u001b[39m keypoints[\u001b[38;5;28;01mNone\u001b[39;00m, :]\n\u001b[1;32m-> 1308\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mkeypoints\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mshape\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m3\u001b[39m:  \u001b[38;5;66;03m# x, y, conf\u001b[39;00m\n\u001b[0;32m   1309\u001b[0m     mask \u001b[38;5;241m=\u001b[39m keypoints[\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m, \u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0.5\u001b[39m  \u001b[38;5;66;03m# points with conf < 0.5 (not visible)\u001b[39;00m\n\u001b[0;32m   1310\u001b[0m     keypoints[\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m, :\u001b[38;5;241m2\u001b[39m][mask] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n",
      "\u001b[1;31mIndexError\u001b[0m: tuple index out of range"
     ]
    }
   ],
   "source": [
    "\n",
    "import cv2\n",
    "import torch\n",
    "import numpy as np\n",
    "from ultralytics import YOLO\n",
    "\n",
    "# 加载YOLOv8模型进行姿态估计\n",
    "model = YOLO('yolov8n-pose.pt')  # 你可以使用YOLOv8的姿态估计模型\n",
    "\n",
    "# 打开视频\n",
    "cap = cv2.VideoCapture(\"video.mp4\")  # 输入你的视频路径\n",
    "\n",
    "while True:\n",
    "    ret, frame = cap.read()\n",
    "    if not ret:\n",
    "        break\n",
    "    \n",
    "    # 通过模型获取预测结果\n",
    "    results = model(frame)\n",
    "\n",
    "    # 假设 results 是一个列表，并且每个元素都有 keypoints 属性\n",
    "    for result in results:\n",
    "        if hasattr(result, 'keypoints'):\n",
    "            keypoints = result.keypoints\n",
    "\n",
    "            # 打印keypoints的形状，检查数据结构\n",
    "            print(\"Keypoints shape:\", keypoints.shape)\n",
    "            \n",
    "            # 如果关键点数据形状是 (num_keypoints, 3)，表示每个关键点 (x, y, conf)\n",
    "            if keypoints is not None and keypoints.shape[1] == 3:\n",
    "                for i in range(keypoints.shape[0]):  # 遍历所有关键点\n",
    "                    x, y, conf = keypoints[i]  # 获取x, y, conf\n",
    "                    if conf > 0.5:  # 判断关键点的置信度\n",
    "                        cv2.circle(frame, (int(x), int(y)), 5, (0, 0, 255), -1)  # 画圆圈标记关键点\n",
    "\n",
    "        # 绘制检测框\n",
    "        if hasattr(result, 'boxes'):\n",
    "            for box in result.boxes.xyxy[0]:\n",
    "                x1, y1, x2, y2 = map(int, box)\n",
    "                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)  # 绘制检测框\n",
    "    \n",
    "    # 显示帧\n",
    "    cv2.imshow(\"Pose Tracking\", frame)\n",
    "    \n",
    "    # 按 'q' 键退出\n",
    "    if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "        break\n",
    "\n",
    "# 释放资源\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfb67926-7f5c-4a08-917f-79420a0a5eaa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
